#!/usr/bin/env python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

from __future__ import with_statement

__all__ = ['installprogrammingtool']

import os, sys
import tempfile
import xdg.DesktopEntry
import softwaredependent as D
from softwareindependent import TempOwn
import installintelcompiler

def eclipseicon():
	e = xdg.DesktopEntry.DesktopEntry( os.path.abspath('eclipse.desktop') )
	e.set('Name','Eclipse')
	e.set('Exec','/opt/eclipse/eclipse -vmargs -Xms512M -Xmx1024M -Dsun.java2d.opengl=true') #使用OpenGL渲染2D ^_^
	e.set('Icon','/opt/eclipse/plugins/org.eclipse.sdk_3.5.0.v200906111540/eclipse48.png')
	e.set('Terminal','false')
	e.set('Type','Application')
	e.set('Categories','Development;')
	e.set('StartupNotify','true')
	e.write()
	D.execute("sudo mv eclipse.desktop /usr/share/applications/")

#No need in Eclipse 3.4.2
def eclipsejavahome():
	with TempOwn("/etc/eclipse/java_home") as o:
		with open("/etc/eclipse/java_home", "w") as f:
			f.write("/usr/lib/jvm/default-java\n")

#No need in Eclipse 3.4.2
def eclipsestuff():
	#Sometimes Eclipse want the user to do these.
	D.execute("sudo touch /usr/local/lib/eclipse/.eclipseextension")
	D.execute("sudo chmod 2775 /usr/local/lib/eclipse/.eclipseextension")
	D.execute("sudo chown root:staff /usr/local/lib/eclipse/.eclipseextension")

def eclipse():
	e1=D.download("eclipse")
	e2=D.download("eclangpack")
	e4=D.download("eccdt")
	e5=D.download("ecpydev")
	if D.execute("cd /opt/;"
"sudo rm eclipse -rf;"
"sudo tar xzf %s;"
"sudo unzip -qo %s;"
"sudo unzip -qo %s -d eclipse;"
"sudo unzip -qo %s -d eclipse;"
"sudo chmod -R a+r eclipse"%(e1,e2,e4,e5) ):
		eclipseicon()

def pbc():
	fdev=D.download("pbcdev")
	f=D.download("pbc")
	D.execute("sudo dpkg --install %s"%f)
	D.execute("sudo dpkg --install %s"%fdev)

def kscope():
	t = tempfile.NamedTemporaryFile(mode='w')
	t.write("y\n")
	t.flush()
	#kscope在9.04中工作正常 :)无需再去除BUG
	f=D.download("kscope")
	D.execute("sudo gdebi %s < %s"%(f,t.name) )

def programmingpackages():
	D.install("build-essential manpages-dev manpages-posix-dev exuberant-ctags libgmp3c2 libgmp3-dev libncurses5-dev libqt3-mt-dev subversion git-core geany")

def installprogrammingtool():
	programmingpackages()
	kscope()
	eclipse()
	pbc()
	installintelcompiler.installintelcompiler()

if __name__ == "__main__":
	installprogrammingtool()
